function $(id) {
	return document.getElementById(id);
}

function getChild(obj, tagName, id) {
	var children = obj.getElementsByTagName(tagName);
	for (var i=0; i<children.length; ++i) {
		var child = children[i];
		if (child.id.length > 0 && child.id == id) {
			return child;
		}
	}
	return null;
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

function toggleValue(elem, val1, val2) {
	elem.value = elem.value == val1 ? val2 : val1;
}

function votetopic_callback(result) {
	result = result.replace('\n', '');
	var topicId = trim(result.split("|")[0]);
	var userAnswer = trim(result.split("|")[1]);
	var answers = trim(result.split("|")[2]);
	var answer1Votes = answers.split(",")[0] * 1;
	var answer2Votes = answers.split(",")[1] * 1;
	var totalVotes = answer1Votes + answer2Votes;
	var answer1Percentage = 0;
	var answer2Percentage = 0;
	if (totalVotes > 0) {
		answer1Percentage = (answer1Votes / totalVotes) * 100;
		answer2Percentage = (answer2Votes / totalVotes) * 100;
	}
	var answer1ClassName = 'lose';
	var answer2ClassName = 'lose';
	if (answer1Votes > answer2Votes) answer1ClassName = 'win';
	else if (answer1Votes < answer2Votes) answer2ClassName = 'win';
	var votetopic_answer1 = $('votetopic'+topicId+'-answer1');
	var votetopic_answer2 = $('votetopic'+topicId+'-answer2');
	var votedtopic_answer1 = $('votedtopic'+topicId+'-answer1');
	var votedtopic_answer2 = $('votedtopic'+topicId+'-answer2');
	var votedtopic_answer1_votes = $('votedtopic'+topicId+'-answer1-votes');
	var votedtopic_answer2_votes = $('votedtopic'+topicId+'-answer2-votes');
	var votedtopic_answer1_voted = $('votedtopic'+topicId+'-answer1-voted');
	var votedtopic_answer2_voted = $('votedtopic'+topicId+'-answer2-voted');
	var votedtopic_answer1_percentage = $('votedtopic'+topicId+'-answer1-percentage');
	var votedtopic_answer2_percentage = $('votedtopic'+topicId+'-answer2-percentage');
	votedtopic_answer1_votes.innerHTML = answer1Votes;
	votedtopic_answer2_votes.innerHTML = answer2Votes;
	votedtopic_answer1_percentage.style.width = answer1Percentage + '%';
	votedtopic_answer2_percentage.style.width = answer2Percentage + '%';
	votetopic_answer1.style.display = 'none';
	votetopic_answer2.style.display = 'none';
	if (userAnswer == 1) {
		votedtopic_answer2_voted.style.display = 'none';
		votedtopic_answer1_voted.src = "img/arrow-voted-" + answer1ClassName + ".gif";
		answer1ClassName += " voted";
	}
	if (userAnswer == 2) {
		votedtopic_answer1_voted.style.display = 'none';
		votedtopic_answer2_voted.src = "img/arrow-voted-" + answer2ClassName + ".gif";
		answer2ClassName += " voted";
	}
	votedtopic_answer1.className = answer1ClassName;
	votedtopic_answer2.className = answer2ClassName;
	votedtopic_answer1.style.display = 'block';
	votedtopic_answer2.style.display = 'block';
}